package com.beemans.common.utils

import com.beemans.common.app.CommonConfig
import com.beemans.common.data.bean.ResultResponse
import com.beemans.common.ext.stringCallback
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.tiamosu.fly.http.FlyHttp

/**
 * @author tiamosu
 * @date 2021/8/19.
 */
@Suppress("MemberVisibilityCanBePrivate", "unused")
object CommonReportHelper {
    const val ACTION_APP_INIT = "init"                  //用于打开应用时为第几天

    const val ACTION_REWARD_REQUEST = "reward_request"  //激励视频请求
    const val ACTION_REWARD_SHOW = "reward_show"        //激励视频展示
    const val ACTION_REWARD_CLICK = "reward_click"      //激励视频点击

    const val ACTION_SPLASH_REQUEST = "splash_request"  //开屏请求
    const val ACTION_SPLASH_SHOW = "splash_show"        //开屏展示
    const val ACTION_SPLASH_CLICK = "splash_click"      //开屏点击

    const val ACTION_INSERT_REQUEST = "insert_request"  //插屏请求
    const val ACTION_INSERT_SHOW = "insert_show"        //插屏展示
    const val ACTION_INSERT_CLICK = "insert_click"      //插屏点击

    const val ACTION_NATIVE_REQUEST = "native_request"  //信息流请求
    const val ACTION_NATIVE_SHOW = "native_show"        //信息流展示
    const val ACTION_NATIVE_CLICK = "native_click"      //信息流点击

    const val ACTION_BANNER_REQUEST = "banner_request"  //Banner请求
    const val ACTION_BANNER_SHOW = "banner_show"        //Banner展示
    const val ACTION_BANNER_CLICK = "banner_click"      //Banner点击

    /**
     * 用户行为事件上报服务端
     *
     * @param url 地址
     * @param action 行为动作
     * @param networkPlacementId 广告平台的广告位ID
     * @param adSourceId 广告源id
     * @param placementId TopOn广告位ID
     * @param isPrintEnable 是否输出Log
     * @param onResult 结果回调
     */
    fun reportAction(
        url: String,
        action: String,
        networkPlacementId: String,
        adSourceId: String,
        placementId: String,
        isPrintEnable: Boolean = false,
        onResult: (result: ResultResponse) -> Unit
    ) {
        val number = getActionNum(action)
        LogUtils.iTag("ReportAction", "action:$action   number:$number")

        FlyHttp[url]
            .params("action", action)
            .params("number", number.toString())
            .params("network_placement_id", networkPlacementId)
            .params("ad_source_id", adSourceId)
            .params("placement_id", placementId)
            .retryCount(0)
            .setPrintEnable(isPrintEnable)
            .build()
            .execute(stringCallback(false) {
                onResult { result ->
                    onResult.invoke(result)
                }
            })
    }

    /**
     * 上报设备信息（上报激活），必须在获取用户信息后执行
     */
    fun uploadEquip(
        url: String,
        isPrintEnable: Boolean = false,
        onResult: (result: ResultResponse) -> Unit = {}
    ) {
        //只需上报一次，带有TopOn参数UP_ID
        if (!CommonCacheUtils.isUploadEquipWithUpId) {
            onResult.invoke(ResultResponse(code = 200))
            return
        }

        //TopOn 存储的upid，唯一设备id
        val upId = SPUtils.getInstance("anythink_sdk").getString("UP_ID") ?: ""
        if (!CommonCacheUtils.isUploadEquip && upId.isBlank()) {
            onResult.invoke(ResultResponse(code = 200))
            return
        }

        fun start(oaId: String) {
            FlyHttp.post(url)
                .params("ANDROIDID", CommonConfig.androidId)
                .params("IMEI", CommonConfig.imei)
                .params("OAID", oaId)
                .params("androidid", CommonConfig.androidId)
                .params("imei", CommonConfig.imei)
                .params("oaid", oaId)
                .params("mac", CommonConfig.macAddress)
                .params("upid", upId)
                .retryCount(0)
                .setPrintEnable(isPrintEnable)
                .build()
                .execute(stringCallback(false) {
                    onResult { result ->
                        if (!result.isSuccess()) return@onResult
                        CommonCacheUtils.isUploadEquip = false
                        if (upId.isNotBlank()) {
                            CommonCacheUtils.isUploadEquipWithUpId = false
                        }
                        onResult.invoke(result)
                    }
                })
        }

        UMConfig.getUmOaid { oaId -> start(oaId) }
    }

    /**
     * 埋点上报服务端
     */
    fun tmpStatic(
        url: String,
        action: String,
        ext: String,
        isPrintEnable: Boolean = false,
        onResult: (result: ResultResponse) -> Unit
    ) {
        FlyHttp[url]
            .params("action", action)
            .params("ext", ext)
            .retryCount(0)
            .setPrintEnable(isPrintEnable)
            .build()
            .execute(stringCallback(false) {
                onResult { result ->
                    onResult.invoke(result)
                }
            })
    }

    /**
     * 获取广告操作请求
     */
    private fun getActionNum(action: String): Int {
        return when (action) {
            ACTION_APP_INIT -> {
                CommonCacheUtils.openAppDay
            }
            ACTION_REWARD_REQUEST -> {
                CommonCacheUtils.rewardRequestCount += 1
                CommonCacheUtils.rewardRequestCount
            }
            ACTION_REWARD_SHOW -> {
                CommonCacheUtils.rewardShowCount += 1
                CommonCacheUtils.rewardShowCount
            }
            ACTION_REWARD_CLICK -> {
                CommonCacheUtils.rewardClickCount += 1
                CommonCacheUtils.rewardClickCount
            }
            ACTION_SPLASH_REQUEST -> {
                CommonCacheUtils.splashRequestCount += 1
                CommonCacheUtils.splashRequestCount
            }
            ACTION_SPLASH_SHOW -> {
                CommonCacheUtils.splashShowCount += 1
                CommonCacheUtils.splashShowCount
            }
            ACTION_SPLASH_CLICK -> {
                CommonCacheUtils.splashClickCount += 1
                CommonCacheUtils.splashClickCount
            }
            ACTION_INSERT_REQUEST -> {
                CommonCacheUtils.insertRequestCount += 1
                CommonCacheUtils.insertRequestCount
            }
            ACTION_INSERT_SHOW -> {
                CommonCacheUtils.insertShowCount += 1
                CommonCacheUtils.insertShowCount
            }
            ACTION_INSERT_CLICK -> {
                CommonCacheUtils.insertClickCount += 1
                CommonCacheUtils.insertClickCount
            }
            ACTION_NATIVE_REQUEST -> {
                CommonCacheUtils.nativeRequestCount += 1
                CommonCacheUtils.nativeRequestCount
            }
            ACTION_NATIVE_SHOW -> {
                CommonCacheUtils.nativeShowCount += 1
                CommonCacheUtils.nativeShowCount
            }
            ACTION_NATIVE_CLICK -> {
                CommonCacheUtils.nativeClickCount += 1
                CommonCacheUtils.nativeClickCount
            }
            ACTION_BANNER_REQUEST -> {
                CommonCacheUtils.bannerRequestCount += 1
                CommonCacheUtils.bannerRequestCount
            }
            ACTION_BANNER_SHOW -> {
                CommonCacheUtils.bannerShowCount += 1
                CommonCacheUtils.bannerShowCount
            }
            ACTION_BANNER_CLICK -> {
                CommonCacheUtils.bannerClickCount += 1
                CommonCacheUtils.bannerClickCount
            }
            else -> 1
        }
    }
}